<%@page import="com.zhuoshi.bean.AreaBean"%>
<%@page import="com.zhuoshi.dao.AreaDao"%>
<%@page import="com.zhuoshi.utils.DateHelper"%>
<%@page import="com.zhuoshi.dao.ManagerDao"%>
<%@page import="com.zhuoshi.bean.ManagerBean"%>
<%@page import="java.sql.Date"%>
<%@page import="com.zhuoshi.utils.DESEncrypt"%>
<%@page import="com.zhuoshi.utils.Utils"%>
<%@page import="com.zhuoshi.dao.ManagerRoleDao"%>
<%@page import="com.zhuoshi.bean.ManagerRoleBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% request.setCharacterEncoding("UTF-8"); %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//绑定角色（职位）
ManagerRoleDao roleDao = new ManagerRoleDao();
List<ManagerRoleBean> roleList = roleDao.getList();
pageContext.setAttribute("roleList", roleList);


//绑定区域

AreaDao areaDao = new AreaDao();
List<AreaBean> areaList = areaDao.getList();
pageContext.setAttribute("areaList", areaList);

//绑定省

%>

<%
String action = request.getParameter("action");
if("add".equals(action)){
	ManagerRoleDao rDao = new ManagerRoleDao();
	String username = request.getParameter("username");
	String password = DESEncrypt.encryptMD5(request.getParameter("password"));
	String realname = request.getParameter("realname");
	String gender = request.getParameter("gender");
	String role_id = request.getParameter("role_id");
	int role_type = rDao.getRoleType(rDao.getRoleType(Integer.parseInt(role_id)));
	String telphone = request.getParameter("telphone");
	String email = request.getParameter("email");
	String area = request.getParameter("area");
	String province = request.getParameter("province");
	int enabled = 0;
	String lasttime = Utils.getNowDate();
	
	ManagerBean managerBean = new ManagerBean();
	managerBean.setUsername(username);
	managerBean.setPassword(password);
	managerBean.setRealname(realname);
	managerBean.setGender(gender);
	managerBean.setRole_id(Integer.parseInt(role_id));
	managerBean.setTelphone(telphone);
	managerBean.setEmail(email);
	managerBean.setArea(Integer.parseInt(area));
	managerBean.setProvince(Integer.parseInt(province));
	managerBean.setEnabled(enabled);
	managerBean.setRole_type(role_type);
	managerBean.setLasttime(DateHelper.strToDate(lasttime));
	
	ManagerDao manDao = new ManagerDao();
	int rows = manDao.add(managerBean);
	if(rows>0){
		out.print("<script language='javascript'>alert('添加成功！');location.href='user_add.jsp';window.close();</script>");
	}else{
		out.print("<script language='javascript'>alert('添加失败！');</script>");
	}
}

 %>

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="Bookmark" href="http://localhost:8080/favicon.ico">
<link rel="Shortcut Icon" href="http://localhost:8080/favicon.ico">
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<script type="text/javascript" src="lib/PIE_IE678.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="template/H-ui.min.css">
<link rel="stylesheet" type="text/css" href="template/H-ui.admin.css">
<link rel="stylesheet" type="text/css" href="template/iconfont.css">
<link rel="stylesheet" type="text/css" href="template/icheck.css">
<link rel="stylesheet" type="text/css" href="template/skin.css" id="skin">
<link rel="stylesheet" type="text/css" href="template/style.css">


<title>添加人员 </title>




<script type="text/javascript">
function test()
	{
		var username = document.getElementById('username').value;
		if(username=="")
		{
			document.form.username.focus();
			return false;
		}
		var password = document.getElementById('password').value;
		if(password=="")
		{
			document.form.password.focus();
			return false;
		}
		var name = document.getElementById('realname').value;
		if(name=="")
		{
			document.form.name.focus();
			return false;
		}
		var sex = document.getElementById('sex').value;
		if(sex=="")
		{
			document.form.sex.focus();
			return false;
		}
		var job = document.getElementById('job').value;
		if(job=="")
		{
			document.form.job.focus();
			return false;
		}
		var tel = document.getElementById('tel').value;
		if(tel=="")
		{
			document.form.tel.focus();
			return false;
		}
		var email = document.getElementById('email').value;
		if(email=="")
		{
			document.form.email.focus();
			return false;
		}
		var area = document.getElementById('area').value;
		if(area=="")
		{
			document.form.area.focus();
			return false;
		}
		var province = document.getElementById('province').value;
		if(province=="")
		{
			document.form.province.focus();
			return false;
		}
		return true;
  }
</script>
<script>
/* var xhr;

function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		xhr = new XMLHttpRequest();
	}
}
function checkState(){
	if(xhr.status==200){
		if(xhr.readyState==4){
			document.getElementById("errmsg").innerHTML = xhr.responseText;
		}
	}
}

function checkRepeat(){
	createXMLHttpRequest();
	xhr.onreadystatechange = checkState();
	var username = document.getElementById("username").value;
	xhr.open("get", "uservalid.jsp?username="+username,true);
	xhr.send(null);
} */
</script>
<link rel="stylesheet" href="template/layer.css" id="layui_layer_skinlayercss"><link href="template/WdatePicker.css" rel="stylesheet" type="text/css"></head>

<body>
<article class="page-container">
	<form action="?action=add" enctype="application/x-www-form-urlencoded" name="form" method="post" class="form form-horizontal" id="form-member-add" onSubmit="return test(this)" novalidate="novalidate">
		
		<div class="row cl">
			
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">* </span> 用户名：</label>
			<div class="formControls col-xs-8 col-sm-9">
			<input type="text" class="input-text" style="width:350px;" placeholder="" id="username" name="username" /> 
			<span id="errmsg"></span>
			</div>
		</div>
		
		<div class="row cl">
			<!-- <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户密码:</label> -->
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">* </span> 用户密码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" style="width:350px;" placeholder="" id="password" name="password">
			</div>
		</div>
			
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户姓名：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" style="width:350px;" value="" placeholder="" id="realname" name="realname">
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>性别：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<select type="text" class="input-text" style="width:350px;" placeholder="" name="gender" id="gender">
					<option value="">-------请选择性别-------</option>
					<option value="男">男</option>
					<option value="女">女</option>	
				</select>
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>职位：</label>
			<div class="formControls col-xs-8 col-sm-9">
			    <!-- <span class="select-box"> --> 
				<!--<select class="select" size="1" name="job">-->
				    <select type="text" class="input-text" style="width:350px;" placeholder="" name="role_id" id="role_id">
					<option value="">-------请选择职位-------</option>
					<c:forEach var="bean" items="${roleList }">
					<option value="${bean.id }">${bean.role_name }</option>
					</c:forEach>
						
				</select>
				 
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>电话：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" style="width:350px;" value="" placeholder="" name="telphone" id="telphone">
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>E-mail:</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" style="width:350px;" value="" placeholder="" name="email" id="email">
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>区域:</label>
			<div class="formControls col-xs-8 col-sm-9">
		    <select type="text" class="input-text" style="width:350px;" placeholder="" name="area" id="area" onChange="changeProvince()">
               <option value="">-------请选择区域-------</option> 
               <c:forEach items="${areaList }" var="areaBean">
               
               <option value="${areaBean.id }">${areaBean.areaname }</option>  
               
               </c:forEach>
               
           </select>
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>省份:</label>
			<div class="formControls col-xs-8 col-sm-9">
			<select type="text" class="input-text" style="width:350px;" placeholder="" name="province" id="province"> 
            <option value="">-------请选择省份-------</option> 
            </select>  
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				
				<input class="btn btn-primary radius" type="submit" value="  提交  "> 
				<input class="btn btn-danger radius" type="button" value=" 返 回  " id="transmit" />
			</div>
		</div>
		
		

               
	</form>
</article>

<script type="text/javascript" src="template/jquery.min.js"></script> 
<script type="text/javascript" src="template/layer.js"></script>
<script type="text/javascript" src="template/laypage.js"></script> 
<script type="text/javascript" src="template/jquery.icheck.min.js"></script>
<script type="text/javascript" src="template/WdatePicker.js"></script> 
<script type="text/javascript" src="template/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="template/jquery.validate.min.js"></script> 
<script type="text/javascript" src="template/validate-methods.js"></script> 
<script type="text/javascript" src="template/messages_zh.min.js"></script>
<script type="text/javascript" src="template/H-ui.js"></script><a href="javascript:void(0)" class="Hui-iconfont toTop" title="返回顶部" alt="返回顶部" style="display:none"></a> 
<script type="text/javascript" src="template/H-ui.admin.js"></script> 

 <script type="text/javascript">
    $('#transmit').on('click', function(){
        //刷新页面
        parent.window.location.href='managerlist.jsp';
        //获取窗口索引
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹出层
        parent.layer.close(index);
    });
</script>
 
 
<script type="text/javascript">
	function checkpwd(obj){
	  if(obj.value == "")return false;
	  if(obj.value.match(/[^A-Za-z0-9]/ig)){
	    //alert("密码只能为数字和字母");
	    return false;
	  }
	  return true;
	}
 </script>

<script type="text/javascript">
var xhr;
function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		xhr = new XMLHttpRequest();
	}
	}

	function checkState() {
		if (xhr.status == 200) {
			if (xhr.readyState == 4) {
				//alert("响应");
				document.getElementById("province").innerHTML = xhr.responseText;
			}
		}
	}

	function changeProvince( ){  
      //获取用户选择区域的省份
      createXMLHttpRequest();
	xhr.onreadystatechange = checkState;
	var id = document.getElementById("area").value;
	//alert(id);
	xhr.open("GET", "province.jsp?id="+id,true);
	xhr.send(null);
     
  }  
$(function(){ 
        //文本框失去焦点后 
       $('form :input').blur(function(){ 
            var $parent = $(this).parent(); 
            $parent.find(".formtips").remove(); 
            //验证用户名 
            if( $(this).is('#username') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '用户名不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                    check(this.value);
                   function check(username){
              $.ajax({
            //提交数据的类型 POST GET
            type:"POST",
            //提交的网址
            url:"uservalid.jsp",
            //提交的数据
            data:{username:username},
            //返回数据的格式
            datatype: "text",//"xml", "html", "script", "json", "jsonp", "text".
            //在请求之前调用的函数
            //beforeSend:function(){$("#msg").html("logining");},
            //成功返回之后调用的函数       
                
            success:function(data){
            //alert(data);
            if(data.replace(/\s/g,'')=="此用户不存在"){
            	$parent.append('<span class="formtips onSuccess" style="color:green"></span>');
            }else{
           		$parent.append('<span class="formtips onError" style="color:red">'+data+'</span>');
           		}            
            }  ,
            //调用执行后调用的函数
            complete: function(XMLHttpRequest, textStatus){
            },
            //调用出错执行的函数
            error: function(){
                //请求出错处理
            }         
         });}}} 
            
            //用户密码
            if( $(this).is('#password') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '用户密码不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else if(checkpwd(this) == false ){
            	   var errorMsg = '请输入字母或数字.'; 
            	   $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>');
               }
               else{ 
                      
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            } 
          	//用户姓名
            if( $(this).is('#realname') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '用户姓名不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            } 
          
          	//性别
            if( $(this).is('#gender') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '性别不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            } 
          
          	//职位
            if( $(this).is('#role_id') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '职位不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            } 
            	//电话
            if( $(this).is('#telphone') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '电话不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            } 
             	//email
            if( $(this).is('#email') ){ 
               if( this.value==""  ){ 
                     var errorMsg = 'E-mail不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            } 
              	//区域
            if( $(this).is('#area') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '区域不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            }
              	//省份
            if( $(this).is('#province') ){ 
               if( this.value==""  ){ 
                     var errorMsg = '省份不能为空.'; 
                     $parent.append('<span class="formtips onError" style="color:red">'+errorMsg+'</span>'); 
               }else{ 
                     
                     $parent.append('<span class="formtips onSuccess" style="color:green"></span>'); 
               } 
            }
       }).keyup(function(){ 
          $(this).triggerHandler("blur"); 
       }).focus(function(){ 
            $(this).triggerHandler("blur"); 
       });//end blur 
 
        
       //提交，最终验证。 
       $('#send').click(function(){ 
               $("form :input.required").trigger('blur'); 
               var numError = $('form .onError').length; 
               if(numError!=0){ 
                   return false;   
		  }else{
		  return true;}
               
       
        }); 
})
</script>

<script type="text/javascript">
$(function(){
	
	$("#form-member-add").validate({
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit();
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
			//parent.$('.btn-refresh').click();
			
		}
	});
});
</script> 


</body></html>